/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.binarylei.jstorm.hello_world;

import backtype.storm.Config;
import backtype.storm.LocalCluster;
import backtype.storm.StormSubmitter;
import backtype.storm.topology.TopologyBuilder;
import com.alibaba.jstorm.client.ConfigExtension;
import com.alibaba.jstorm.utils.JStormUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author JohnFang (xiaojian.fxj@alibaba-inc.com).
 */
public class DeployTopology {
    private final static String TOPOLOGYSPOUTPARALLELISMHINT = "spout.parallel";
    private final static String TOPOLOGYBOLTPARALLELISMHINT = "bolt.parallel";
    private final static String TOPOLOGYNUMS = "topology.nums";
    //number of levels of bolts per topolgy
    private final static String TOPOLOGYBOLTSNUMS = "topology.bolt.nums";
    //message size
    private final static String TOPOLOGYMESSAGESIZES = "topology.messagesize";

    private static Map conf = new HashMap<>();

    public static void main(String[] args) throws Exception {
        String name = "MetricTest";
        int killTopologyTimeout = JStormUtils.parseInt(conf.get(ConfigExtension.TASK_CLEANUP_TIMEOUT_SEC), 180);
        conf.put(ConfigExtension.TASK_CLEANUP_TIMEOUT_SEC, killTopologyTimeout);

        int numWorkers = JStormUtils.parseInt(conf.get(Config.TOPOLOGY_WORKERS), 6);

        int numTopologies = JStormUtils.parseInt(conf.get(TOPOLOGYNUMS), 1);
        int spoutParallel = JStormUtils.parseInt(conf.get(TOPOLOGYSPOUTPARALLELISMHINT), 2);
        int boltParallel = JStormUtils.parseInt(conf.get(TOPOLOGYBOLTPARALLELISMHINT), 4);
        int messageSize = JStormUtils.parseInt(conf.get(TOPOLOGYMESSAGESIZES), 10);
        int numAcker = JStormUtils.parseInt(conf.get(Config.TOPOLOGY_ACKER_EXECUTORS), 2);
        int boltNum = JStormUtils.parseInt(conf.get(TOPOLOGYBOLTSNUMS), 3);
        boolean ackEnabled = false;
        if (numAcker > 0)
            ackEnabled = true;

        for (int topoNum = 0; topoNum < numTopologies; topoNum++) {
            TopologyBuilder builder = new TopologyBuilder();
            builder.setSpout("messageSpout",
                    new DeploySpoult(messageSize, ackEnabled), spoutParallel);
            builder.setBolt("messageBolt1", new DeployBolt(), boltParallel)
                    .shuffleGrouping("messageSpout");
            for (int levelNum = 2; levelNum <= boltNum; levelNum++) {
                builder.setBolt("messageBolt" + levelNum, new DeployBolt(), boltParallel)
                        .shuffleGrouping("messageBolt" + (levelNum - 1));
            }

            conf.put(Config.TOPOLOGY_WORKERS, numWorkers);
            conf.put(Config.TOPOLOGY_ACKER_EXECUTORS, numAcker);

            LocalCluster cluster = new LocalCluster();
            cluster.submitTopology("top1", conf, builder.createTopology());
            Thread.sleep(20 * 1000);
            cluster.killTopology("top1");
            cluster.shutdown();
//            StormSubmitter.submitTopology(name + "" + topoNum, conf, builder.createTopology());
        }
    }

}